#!/bin/bash
#pMusic - build gtkdialog xml code
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
if [ "$1" = "-width" ]; then
	WIDTH_ARTWORK="<width>$2</width>"
	WIDTH_BOX=' width-request="'$2'"'
fi

template(){
	TMP=${1^^}
	echo '
	<text xalign="0" width-request="100" '$SF'>
	 <variable>TXT_'$TMP'</variable>
	 <label>'$2'</label>
	</text>
	<pixmap tooltip-text="'$(gettext 'The icon shows status of auto tag-search (found or searching)')'" '$SF'>
	 <variable>ICON_'$TMP'</variable>
	 <width>20</width>
	 <input file>'$WORKDIR'/trackinfo'${MYPID}'_icon_id3_'$1'.svg</input>
	</pixmap>
	<entry activates-default="true" primary-icon-stock="gtk-clear" '$ST'>
	 <variable>'$TMP'</variable>
	 <input file>'$WORKDIR'/trackinfo'${MYPID}'_'$TMP'</input>
	 <action signal="focus-in-event">'$APPDIR'/func_kill -trackinfo &</action>
	 <action signal="primary-icon-release">echo "" > '$WORKDIR'/trackinfo'${MYPID}'_'$TMP'</action>
	 <action signal="primary-icon-release">refresh:'$TMP'</action>
	</entry>'
}
export -f template

GENRES=""
while read I; do
	GENRES="$GENRES <menuitem><label>"$I"</label><action>echo "${I% (*}" > $WORKDIR/trackinfo${MYPID}_GENRE</action><action>refresh:GENRE</action></menuitem>"
done < $APPDIR/txt_id3genre

S='<timer visible="false">
 <variable>UPDATE_TRACKINFO</variable>
 <action condition="file_is_false( '$WORKDIR'/UPDATE_TRACKINFO'${MYPID}' )" function="break">""</action>
 <action>echo false > '$WORKDIR'/UPDATE_TRACKINFO'${MYPID}'</action>'
 for I in ARTIST TITLE ALBUM ALBUM_DISCOGRAPHY YEAR NR COMMENT GENRE ICON_ARTIST ICON_TITLE ICON_ALBUM ICON_YEAR ICON_NR TXT_ID3_INFO EDIT_LYRICS LYRICS_SOURCE ALBUMART ALBUMART_SOURCE ALBUM_LIST ALBUM_LIST_SOURCE EDIT_DISCOGRAPHY TREE_URLS STREAMINFO; do
	S=$S'<action>refresh:'$I'</action>'
 done
S=$S'</timer>'
GUI_TRACKINFO_UPDATE="$S"

S='
<vbox border-width="10">
 <hbox>
  <text width-request="124"><label>""</label></text>
  <vbox '$SF' homogeneous="true">
   <button tooltip-text="'$(gettext 'Clear tags')'" '$SF'>
    <label>'$(gettext 'Clear tags')'</label>
    <input file stock="gtk-clear"></input>
    <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -clear_tags</action>
   </button>
  </vbox>
  <text '$ST'><label>""</label></text>
  <button can-default="true" has-default="true" tooltip-text="'$(gettext 'After saving new meta-info, you need to click the 'Refresh' button to update info in the other tabs - Lyrics, albumart')'...">
   <label>'$(gettext 'Save tags')'</label>
   <input file stock="gtk-save"></input>
   <action>ln -sf '$APPDIR'/themes/$THEME/blank.svg $WORKDIR/trackinfo'${MYPID}'_icon_id3_artist.svg</action>
   <action>ln -sf '$APPDIR'/themes/$THEME/blank.svg $WORKDIR/trackinfo'${MYPID}'_icon_id3_title.svg</action>
   <action>ln -sf '$APPDIR'/themes/$THEME/blank.svg $WORKDIR/trackinfo'${MYPID}'_icon_id3_album.svg</action>
   <action>ln -sf '$APPDIR'/themes/$THEME/blank.svg $WORKDIR/trackinfo'${MYPID}'_icon_id3_year.svg</action>
   <action>ln -sf '$APPDIR'/themes/$THEME/blank.svg $WORKDIR/trackinfo'${MYPID}'_icon_id3_nr.svg</action>
   <action>refresh:ICON_ARTIST</action>
   <action>refresh:ICON_TITLE</action>
   <action>refresh:ICON_ALBUM</action>
   <action>refresh:ICON_YEAR</action>
   <action>refresh:ICON_NR</action>
   <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -save_tags</action>
  </button>
 </hbox>

 <hseparator></hseparator>
 <text><label>""</label></text>
 
 <hbox>
  <vbox '$ST'>
   <hbox '$ST'>
    '$(template artist "$(gettext 'Artist')")'
   </hbox>
   <hbox '$ST'>
    '$(template title "$(gettext 'Title')")'
   </hbox>
  </vbox>
  <button tooltip-text="'$(gettext 'Search for alternative meta information based on ARTIST and TITLE. The search-string does not require complete Artist/Title information, but you MUST use complete words.')'">
   <input file stock="gtk-find"></input>
   <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -search_alternatives</action>
   <action>gtkdialog -f '$WORKDIR'/xml_pmusic_trackinfo'${MYPID}'_alternativ_id3 &</action>
  </button>
 </hbox>
 
 <text><label>""</label></text>
 
 <hbox>
  <vbox '$ST'>
   <hbox '$ST'>
    '$(template album "$(gettext 'Album')")'
   </hbox>

   <hbox visible="false" '$ST'>
    <text width-request="20" '$SF'><label>""</label></text>
    <tree exported_column="2" sort-function="1" column-visible="true|true|false" hover-selection="true" '$ST'>
     <label>|'$(gettext 'Pick correct album')'|musicbrainz.org Disc-id</label>
     <variable>ALBUM_DISCOGRAPHY</variable>
     <input file>'$WORKDIR'/trackinfo'${MYPID}'_discography</input>
     <height>150</height>
     <action signal="focus-in-event">'$APPDIR'/func_kill -trackinfo &</action>
     <action signal="button-release-event">cat '$WORKDIR'/trackinfo'${MYPID}'_discography | grep -F ${ALBUM_DISCOGRAPHY} | cut -d"|" -f2 > '$WORKDIR'/trackinfo'${MYPID}'_ALBUM</action>
     <action signal="button-release-event">cat '$WORKDIR'/trackinfo'${MYPID}'_discography | grep -F ${ALBUM_DISCOGRAPHY} | cut -d"|" -f1 | cut -d"-" -f1 > '$WORKDIR'/trackinfo'${MYPID}'_YEAR</action>
     <action signal="button-release-event">echo "" > '$WORKDIR'/trackinfo'${MYPID}'_NR</action>
     <action signal="button-release-event">refresh:ALBUM</action>
     <action signal="button-release-event">refresh:YEAR</action>
     <action signal="button-release-event">refresh:NR</action>
     <action signal="button-release-event">activate:ALBUM_CHOOSER</action>
     <action signal="button-release-event">export INPUT_MB_DISC_ID=$ALBUM_DISCOGRAPHY; . '$APPDIR'/func_id3io "'$FILE'" -read</action>
     <action signal="button-release-event">. '$APPDIR'/func_trackinfo "'${MYPID}'" -save_tags</action>
    </tree>
    <variable>ALBUM_DISCOGRAPHY_HBOX</variable>
   </hbox>
  </vbox>
  <vbox '$SF'>
   <togglebutton width-request="30" '$SF' tooltip-text="'$(gettext 'Choose album from discography')'">
    <variable>ALBUM_CHOOSER</variable>
    <label>..</label>
    <action>if false hide:ALBUM_DISCOGRAPHY_HBOX</action>
    <action>if false show:TXT_ALBUM</action>
    <action>if false show:ICON_ALBUM</action>
    <action>if false show:ALBUM</action>
    <action>if true show:ALBUM_DISCOGRAPHY_HBOX</action>
    <action>if true hide:TXT_ALBUM</action>
    <action>if true hide:ICON_ALBUM</action>
    <action>if true hide:ALBUM</action>
   </togglebutton>
  </vbox>
 </hbox>
  
 <hbox>
  '$(template year "$(gettext 'Date')")'
  <text width-request="30"><label>""</label></text>
 </hbox>
 <hbox>
  '$(template nr "$(gettext 'Track nr')")'
  <text width-request="30"><label>""</label></text>
 </hbox>
 <hbox>
  <text width-request="124"><label>'$(gettext 'Genre')'</label></text>
  <entry activates-default="true" primary-icon-stock="gtk-clear">
   <variable>GENRE</variable>
   <input>cat '$WORKDIR'/trackinfo'${MYPID}'_GENRE</input>
   <action signal="focus-in-event">'$APPDIR'/func_kill -trackinfo &</action>
   <action signal="primary-icon-release">echo "" > '$WORKDIR'/trackinfo'${MYPID}'_GENRE</action>
   <action signal="primary-icon-release">refresh:GENRE</action>
  </entry>
   <menubar width-request="30">
    <menu>
    '$GENRES'
   <label>..</label>
    </menu>
   </menubar>
 </hbox>
 <hbox>
  <text width-request="124"><label>'$(gettext 'Comment')'</label></text>
  <entry activates-default="true" primary-icon-stock="gtk-clear">
   <variable>COMMENT</variable>
   <input>cat '$WORKDIR'/trackinfo'${MYPID}'_COMMENT</input>
   <action signal="focus-in-event">'$APPDIR'/func_kill -trackinfo &</action>
   <action signal="primary-icon-release">echo "" > '$WORKDIR'/trackinfo'${MYPID}'_COMMENT</action>
   <action signal="primary-icon-release">refresh:COMMENT</action>
  </entry>
  <text width-request="30"><label>""</label></text>
 </hbox>
 <vbox '$ST'>
  <text'$WIDTH_BOX'>
   <variable>TXT_ID3_INFO</variable>
   <input>cat '$WORKDIR'/trackinfo'${MYPID}'_ID3_info</input>
  </text>
 </vbox>
</vbox>'
GUI_TRACKINFO_ID3="$S"






#####      L Y R I C S      ######


S='
<vbox border-width="10">
 <hbox>
  <entry name="lyrics_path" editable="false" progress-fraction="1">
   <variable>LYRICS_SOURCE</variable>
   <input>cat '$WORKDIR'/trackinfo'${MYPID}'_lyrics_source</input>
  </entry>
  <button tooltip-text="'$(gettext 'Edit local file')'">'
   [ $LYRICS_LOCAL = false ] && S=$S'<sensitive>false</sensitive>'
   S=$S'<input file stock="gtk-edit"></input>
   <action>if [ ! "`echo "$LYRICS_SOURCE" | grep -F "/"`" ]; then TMP="'$STORAGE_DIR'/lyrics/$ARTIST - ${TITLE}.txt"; echo -n > "$TMP"; else TMP=$LYRICS_SOURCE; fi; export GTK2_RC_FILES=$HOME/.gtkrc-2.0; '$TEXT_EDITOR' "$TMP" &</action>
  </button>
  <togglebutton tooltip-text="'$(gettext ''$(gettext 'Manually override default search order.')'')'">
   <input file stock="gtk-refresh"></input>
   <action>if true show:LYRICS_REFRESH</action>
   <action>if false hide:LYRICS_REFRESH</action>
  </togglebutton>
  <button tooltip-text="'$(gettext 'Local storage')'">
   <input file stock="gtk-preferences"></input>
   <action>'$APPDIR'/func_config -preferences -lyrics</action>
  </button>
 </hbox>
 <vbox visible="false">
  <hseparator></hseparator>
  <hbox '$SF'>
   <button '$SF'>
    <label>'$(gettext 'lyrics.wikia.com')'</label>
    <input file stock="gtk-refresh"></input>
    <action>echo "" > $WORKDIR/trackinfo${MYPID}_lyrics_source</action>
    <action>refresh:LYRICS_SOURCE</action>
    <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -lyrics_refresh lyricswiki &</action>
    <action>echo</action>
   </button>
   <button '$SF'>
    <label>'$(gettext 'elyrics.net')'</label>
    <input file stock="gtk-refresh"></input>
    <action>echo "" > $WORKDIR/trackinfo${MYPID}_lyrics_source</action>
    <action>refresh:LYRICS_SOURCE</action>
    <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -lyrics_refresh elyrics &</action>
   </button>
   <text '$ST'><label>""</label></text>
  </hbox>
  <variable>LYRICS_REFRESH</variable>
 </vbox>
 <vbox '$ST'> 
  <edit name="lyrics" left_margin="10" cursor-visible="false">
   <variable>EDIT_LYRICS</variable>
   <input file>'$WORKDIR'/trackinfo'${MYPID}'_lyrics</input>
  </edit>
 </vbox>
</vbox>'
GUI_TRACKINFO_LYRICS="$S"






#####      A L B U M   A R T      ######


GUI_TRACKINFO_ALBUMART='
<vbox border-width="10">
 <hbox>
  <entry name="albumart_path" editable="false" progress-fraction="1">
   <variable>ALBUMART_SOURCE</variable>
   <input file>'$WORKDIR'/trackinfo'${MYPID}'_albumart_source</input>
  </entry>
  <button tooltip-text="'$(gettext 'Save')'">
   <variable>ALBUMART_SAVE</variable>
   <input file stock="gtk-save"></input>
   <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -save_albumart</action>
  </button>
  <button tooltip-text="'$(gettext 'Delete embedded albumart')'">
   <variable>ALBUMART_DEL</variable>
   <input file stock="gtk-delete"></input>
   <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -del_albumart</action>
  </button>
  <togglebutton tooltip-text="'$(gettext 'Manually override default search order.')'">
   <input file stock="gtk-find_more"></input>
   <action>if true show:ALBUMART_REFRESH</action>
   <action>if false hide:ALBUMART_REFRESH</action>
  </togglebutton>
  <button tooltip-text="'$(gettext 'Local storage')'">
   <input file stock="gtk-preferences"></input>
   <action>'$APPDIR'/func_config -preferences -lyrics</action>
  </button>
 </hbox>
 <vbox visible="false">
  <hseparator></hseparator>
  <hbox '$SF'>
   <togglebutton tooltip-text="'$(gettext 'Manually search the web.')'">
    <label>'$(gettext 'www Manual')'</label>
    <input file stock="gtk-find_more"></input>
    <action>if true show:ALBUMART_WEB</action>
    <action>if false hide:ALBUMART_WEB</action>
   </togglebutton>
   <button '$SF' tooltip-text="'$(gettext 'Use builtin albumart-grabber to find albumart on the web.')'">
    <label>'$(gettext 'www Auto')'</label>
    <input file stock="gtk-find"></input>
    <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -albumart_refresh amazon</action>
   </button>
   <button '$SF'>
    <label>'$(gettext 'Local file')'</label>
    <input file stock="gtk-open"></input>
    <action type="fileselect">ALBUMART_SOURCE</action>
    <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -albumart_refresh "$ALBUMART_SOURCE"</action>
   </button>
   <button '$SF' tooltip-text="'$(gettext 'Check if audio file has an embedded image')'">
    <label>'$(gettext 'Embedded')'</label>
    <input file stock="gtk-find"></input>
    <action>echo " " > '$WORKDIR'/trackinfo'${MYPID}'_albumart_source</action>
    <action>refresh:ALBUMART_SOURCE</action>
    <action>'$APPDIR'/func_trackinfo "'${MYPID}'" -albumart_refresh embedded</action>
   </button>
   <text width-request="1" '$ST'><label>""</label></text>
  </hbox>
  <variable>ALBUMART_REFRESH</variable>
 </vbox>

 <vbox visible="false">
  <hseparator></hseparator>
  <hbox '$SF'>
   <button '$SF'>
    <label>'$(gettext 'albumart.org')'</label>
    <action>'$BROWSER' http://www.albumart.org &</action>
   </button>
   <button '$SF'>
    <label>'$(gettext 'amazon.com')'</label>
    <action>'$BROWSER' http://www.amazon.com &</action>
   </button>
   <button '$SF'>
    <label>'$(gettext 'albumartexchange.com')'</label>
    <action>'$BROWSER' http://www.albumartexchange.com/ &</action>
   </button>
   <text width-request="1" '$ST'><label>""</label></text>
  </hbox>
  <text xalign="0" '$ST'><label>"'$(gettext 'This will point you to the chosen webpage to manually do a search for wished albumart. To let the albumart interact with pMusic db, you should save the image to')'"</label></text>
  <text selectable="true" xalign="0" use-markup="true" '$ST'><label>"<b>'${STORAGE_DIR}'/albumart/Artist - Album.jpg</b>"</label></text>
  <variable>ALBUMART_WEB</variable>
 </vbox>



 <vbox scrollable="true" shadow-type="0" '$ST'>
  <eventbox tooltip-text="'$(gettext 'Click for full size')'" above-child="true" visible-window="true" spacing="0" '$ST'>
   <pixmap '$ST'>
    <variable>ALBUMART</variable>
    <input file>'$WORKDIR'/trackinfo'${MYPID}'_albumart.jpg</input>
    '$WIDTH_ARTWORK'
   </pixmap>
   <action signal="button-release-event">albumart_fullsize</action>
  </eventbox>
 </vbox>
</vbox>'


S='
<vbox border-width="10">
 <hbox>
  <entry name="albumlist_path" editable="false" progress-fraction="1">
   <variable>ALBUM_LIST_SOURCE</variable>
   <input>cat '$WORKDIR'/trackinfo'${MYPID}'_album_source</input>
  </entry>
  <button tooltip-text="'$(gettext 'Edit local file')'">'
   [ $PMU_LOCAL = false ] && S=$S'<sensitive>false</sensitive>'
   S=$S'<input file stock="gtk-edit"></input>
   <action>if [ ! "`echo "$ALBUM_LIST_SOURCE" | grep -F "/"`" ]; then TMP="'$STORAGE_DIR'/pmu/${ARTIST} - ${ALBUM}.pmu"; echo -n > "$TMP"; else TMP="$ALBUM_LIST_SOURCE"; fi; export GTK2_RC_FILES=$HOME/.gtkrc-2.0; '$TEXT_EDITOR' "$TMP" &</action>
  </button>
  <button tooltip-text="'$(gettext 'Clear album list and make a new search')'">
   <input file stock="gtk-refresh"></input>
   <action signal="button-press-event">clear:ALBUM_LIST</action>
   <action>echo " " > '$WORKDIR'/trackinfo'${MYPID}'_album</action>
   <action>TMP="$ALBUM_LIST_SOURCE"; rm "$TMP"</action>
   <action>'$APPDIR'/func_kill -trackinfo</action>
   <action>. '$APPDIR'/func_trackinfo '${MYPID}' -quiet -refresh</action>
  </button>
  <button tooltip-text="'$(gettext 'Local storage')'">
   <input file stock="gtk-preferences"></input>
   <action>'$APPDIR'/func_config -preferences -lyrics</action>
  </button>
 </hbox>
 <tree rubber-banding="true" fixed-height-mode="true" selection-mode="3" exported_column="4" column-visible="false|true|true|true|false">
  <label>|'$(gettext 'Length')'|'$(gettext 'Title')'|'$(gettext 'Path')'|</label>
  <variable>ALBUM_LIST</variable>
  <input file>'$WORKDIR'/trackinfo'${MYPID}'_album</input>
  <action signal="button-press-event">if [ $PTR_BTN -eq 3 ]; then echo "$ALBUM_LIST" > '$WORKDIR'/ALBUM_LIST_PRESS_EVENT; . '$APPDIR'/menu_trackinfo_albumlist; elif [ $PTR_BTN -eq 2 ]; then '$APPDIR'/func -play_direct "$ALBUM_LIST"; fi</action>
  <action>TRACK="`echo "$ALBUM_LIST" | cut -c 21-`"; [ ! "$TRACK" ] && TRACK="`grep -F "$ALBUM_LIST" '$WORKDIR'/trackinfo'${MYPID}'_album | cut -d"|" -f3`"; '$APPDIR'/func_add -add "$TRACK"</action>
 </tree>
</vbox>'
GUI_TRACKINFO_ALBUM="$S"


GUI_TRACKINFO_ARTIST='
<vbox border-width="10">
 <text use-markup="true"><label>"<b>'$(gettext 'Discography')'</b>"</label></text>
 <tree sort-function="1" column-visible="true|true|false" selection-mode="0">
  <label>'$(gettext 'Year')'|'$(gettext 'Album')'|musicbrainz.org Disc-id</label>
  <variable>EDIT_DISCOGRAPHY</variable>
  <input file>'$WORKDIR'/trackinfo'${MYPID}'_discography</input>
 </tree>
 <text use-markup="true"><label>"<b>'$(gettext 'Webpages')'</b> - '$(gettext 'doubleclick to browse')'"</label></text>
 <tree exported_column="1">
  <label>'$(gettext 'Type')'|URL</label>
  <variable>TREE_URLS</variable>
  <input>cat '$WORKDIR'/trackinfo'${MYPID}'_urls</input>
  <action>export GTK2_RC_FILES='$HOME'/.gtkrc-2.0; TMP=$TREE_URLS; '$BROWSER' $TMP &</action>
 </tree>
</vbox>'


GUI_TRACKINFO_STREAM='
<vbox border-width="10">
 <tree headers-visible="false" selection-mode="0">
  <label>a|b</label>
  <variable>STREAMINFO</variable>
  <input file>'$WORKDIR'/trackinfo'${MYPID}'_stream</input>
 </tree>
</vbox>'

 echo '
<notebook page="'$TABNR'" labels="'$(gettext 'Tags')'|'$(gettext 'Lyrics')'|'$(gettext 'Album')'|'$(gettext 'Album art')'|'$(gettext 'Artist')'|'$(gettext 'Stream')' " '$ST'>
 '$GUI_TRACKINFO_ID3'
 '$GUI_TRACKINFO_LYRICS'
 '$GUI_TRACKINFO_ALBUM'
 '$GUI_TRACKINFO_ALBUMART'
 '$GUI_TRACKINFO_ARTIST'
 '$GUI_TRACKINFO_STREAM'
</notebook>
<vbox height-request="1" '$SF'>
 '$GUI_TRACKINFO_UPDATE'
</vbox>' > $WORKDIR/xml_pmusic_trackinfo${MYPID}
